package com.yoomiito.app.ui.yijiayou

import cn.droidlover.xdroidmvp.net.ApiException
import cn.droidlover.xdroidmvp.net.ApiSubscriber
import cn.droidlover.xdroidmvp.net.BaseModel
import cn.droidlover.xdroidmvp.net.XApi
import com.yoomiito.app.base.AppConfig
import com.yoomiito.app.base.BaseActPresenter
import com.yoomiito.app.model.oilcard.OilCardData
import com.yoomiito.app.net.YmApi
import com.yoomiito.app.showToast

class OilCardPresenter(appConfig: AppConfig) :BaseActPresenter<OilCardActivity>(appConfig){

    fun actCode(code:String){
        YmApi.getYmService()
                .actOilCardCode(code)
                .compose(v.bindToLifecycle())
                .compose(XApi.getApiTransformerNoData<BaseModel<*>>())
                .compose(XApi.getScheduler())
                .subscribe(object :ApiSubscriber<BaseModel<*>>(){
                    override fun onSuccess(t: BaseModel<*>?) {
                        v.paySuccess()
                    }

                    override fun onFail(error: ApiException?) {
                        v.dismissLoading()
                        error?.msg?.showToast()
                    }

                })
    }

    fun getOilCardInfo(){
        YmApi.getYmService()
                .oilCardInfo()
                .compose(v.bindToLifecycle())
                .compose(XApi.getApiTransformerData())
                .compose(XApi.getScheduler())
                .subscribe(object :ApiSubscriber<OilCardData>(){
                    override fun onSuccess(t: OilCardData?) {
                        v?.setInfo(t)
                    }

                    override fun onFail(error: ApiException?) {
                        v?.setInfo(null)
                    }

                })
    }
}